﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Data.Linq;
using System.Text;
using System.Data.Linq.Mapping;

namespace DomainModel.Entities
{
    // This is the Linq to SQL class for the question table.
    // This is the bottom layer of the DAL.
    [Table (Name = "Questions")]
    public class Question : IDataErrorInfo
    {
        [Column(Name = "questionID", IsPrimaryKey = true, IsDbGenerated = true, AutoSync = AutoSync.OnInsert)]
        public int QuestionID { get; set; }

        [Column(Name = "question")]
        public string QuestionString { get; set; }
        [Column(Name = "question_type")]
        public Int16 Question_Type { get; set; }
        [Column(Name = "minimum_score")]
        public int Minimum_Score { get; set; } 

        [Association(OtherKey = "QuestionID")] // One to many relationship for questions -> answers.
        private EntitySet<Answer> _answers = new EntitySet<Answer>();
        public IList<Answer> Answers { get { return _answers.ToList().AsReadOnly(); } }

        [Association(OtherKey = "QuestionID")] // Many to many relationship for questions <-> jobs.
        private EntitySet<QuestionnaireEntry> _questionnaireEntries = new EntitySet<QuestionnaireEntry>();
        public IList<QuestionnaireEntry> QuestionnaireEntries { get { return _questionnaireEntries.ToList().AsReadOnly(); } }

        public string this[string fieldName]
        {
            get
            {
                if (((fieldName == "QuestionString") && string.IsNullOrEmpty(QuestionString)) || 
                    ((fieldName == "QuestionString") && (QuestionString.Length > 8000)))
                    return "Question must not be empty or longer than 8000 characters.";

                if (((fieldName == "Minimum_Score") && (Minimum_Score > 5)) ||
                    ((fieldName == "Minimum_Score") && (Minimum_Score < -5)))
                    return "Minumum score must be between -5 and 5.";

                return null;
            }
        }

        public string Error { get { return null; } }
    }
}
